import gameobject
from gameobject import *

import pygame

import random

from pygame import *

class Player(GameObject):
    def __init__(self, position, spritesheet, spritepos, itemspritesheet):
        GameObject.__init__(self)
        self.type = "player"
        self.position = position
        self.rect = Rect(0.0,0.0,8.0,8.0)
        self.rect.center = self.position
        self.frames = [[0,0,0,0],[0,0,0,0]]
        self.scaledframes = [[0,0,0,0],[0,0,0,0]]
        self.facingright = True
        self.speed = .3
        self.animationcountdown = self.speed
        self.blinkingcountdown = self.speed / 3.0
        self.frame = 0
        self.velocity = 140.0
        self.keyMap={"left":False, "right":False, "up":False,"down":False, "attack": False}
        self.blinking = False
        self.blinkingonoff = False
        self.lastdirection = 4
        self.colorframe = 3
        self.canattack = True
        self.damage = 1.0
        self.attackspeed = 380.0
        self.attackrate = .15
        self.attackcountdown = 0.0
        self.neighboringdirections = [[7,0,1],[0,1,2],[1,2,3],[2,3,4],[3,4,5],[4,5,6],[5,6,7],[6,7,0]]
        self.directionsprites = [[Surface((8.0,8.0)) for i in range(8)] for j in range(2)]
        self.score = 0
        self.combo = 1.0
        self.health = 100.0
        self.perceivedhealth = 100.0
        self.itemscollected = []
        self.dead = False
        for i in range(2):
            for j in range(len(self.directionsprites[i])):
                self.directionsprites[i][j].set_colorkey((0,255,255))
                self.directionsprites[i][j].blit(itemspritesheet, (0,0), ((j*8, (i+10)*8),(8,8)))
                for x in range(self.directionsprites[i][j].get_width()):
                    for y in range(self.directionsprites[i][j].get_height()):
                        currentcolor = self.directionsprites[i][j].get_at((x,y))

                        if currentcolor == (0,0,0, 255):
                            self.directionsprites[i][j].set_at((x,y),(74,81,57))
                           
                self.directionsprites[i][j] = transform.scale(self.directionsprites[i][j], (32,32))

        for i in range(2):
            for j in range(4):
                self.frames[i][j] = Surface((8,8))
                self.frames[i][j].set_colorkey((0,255,255))
                self.frames[i][j].blit(spritesheet, (0,0), ((spritepos[0]*8)+(96*i),(spritepos[1]*8),8,8))
            for x in range(self.frames[i][0].get_width()):
                for y in range(self.frames[i][0].get_height()):
                    currentcolor = self.frames[i][0].get_at((x,y))

                    if currentcolor == (0,0,0, 255):
                        self.frames[i][0].set_at((x,y),(196,207,161))
                        self.frames[i][1].set_at((x,y),(139,149,109))
                        self.frames[i][2].set_at((x,y),(74,81,57))
                        self.frames[i][3].set_at((x,y),(31,31,31,255))
                        
                        
                        

        for i in range(2):
            for j in range(4):
                self.scaledframes[i][j] = transform.scale(self.frames[i][j], (self.frames[i][j].get_width()*4,self.frames[i][j].get_height()*4))

    def update(self, etime, camera):
        if self.perceivedhealth > self.health:
            self.perceivedhealth -= etime*4
        self.attackcountdown -= etime

        self.animationcountdown -= etime
        if self.animationcountdown <= 0.0:
            self.frame = 1 if self.frame == 0 else 0
            self.animationcountdown = self.speed*(1.0+random.random()/4)
        if self.blinkingonoff:
            #print self.blinkingcountdown
            self.blinkingcountdown -= etime
            if self.blinkingcountdown <= 0.0:
                self.blinking = True if self.blinking == False else False
                self.blinkingcountdown = .05*(3.0+random.random())
                #print "blink"
        else:
            self.blinking = False
            self.blinkingcountdown = 0.0
        if self.health < 0.0 and not self.blinkingonoff:
            self.blinkingonoff = True
            self.blinkingcountdown = .3
            self.dead = True
            return
        currentx = self.position[0]
        currenty = self.position[1]
        keydown = False
        if self.keyMap["left"]:
            currentx -= self.velocity*(etime)
            self.facingright = False
            keydown = True
            if currentx < -508:
                currentx = -508
        elif self.keyMap["right"]:
            currentx+= self.velocity*(etime)
            if currentx > 482:
                currentx = 482
            self.facingright = True
            keydown = True
        if self.keyMap["up"]:
            currenty -= self.velocity*(etime)
            keydown = True
            if currenty < -234:
                currenty = -234
        elif self.keyMap["down"]:
            currenty+= self.velocity*(etime)
            keydown = True
            if currenty > 260:
                currenty = 260
        if self.keyMap["attack"]:
            pass

        currentdirection = -1
        if self.keyMap["left"]:
            if self.keyMap["up"]: currentdirection = 7
            elif self.keyMap["down"]: currentdirection = 5
            else: currentdirection = 6
        elif self.keyMap["right"]:
            if self.keyMap["up"]: currentdirection = 1
            elif self.keyMap["down"]: currentdirection = 3
            else: currentdirection = 2
        else:
            if self.keyMap["up"]: currentdirection = 0
            elif self.keyMap["down"]: currentdirection = 4
            else: currentdirection = -1
        if currentdirection != -1:
            self.lastdirection = currentdirection
        
        """
        if keydown == True:
            self.colorframe = 3
        else:
            self.colorframe = 2
        """
        
        self.position = (currentx, currenty)



        

        self.rect.center = self.position
        self.rect.inflate(32.0, 32.0)
        
        self.drawrect = self.rect.copy()
        self.drawrect.center = self.position
        self.drawrect.left -= camera[0] 
        self.drawrect.top -= camera[1]
        
        self.dirmarkpositions = [(self.drawrect.topleft) for i in range(8)]
        self.dirmarkpositions[0] = (self.dirmarkpositions[0][0],self.dirmarkpositions[0][1]-32)
        self.dirmarkpositions[1] = (self.dirmarkpositions[1][0]+32,self.dirmarkpositions[1][1]-32)
        self.dirmarkpositions[2] = (self.dirmarkpositions[2][0]+32,self.dirmarkpositions[2][1])
        self.dirmarkpositions[3] = (self.dirmarkpositions[3][0]+32,self.dirmarkpositions[3][1]+32)
        self.dirmarkpositions[4] = (self.dirmarkpositions[4][0],self.dirmarkpositions[4][1]+32)
        self.dirmarkpositions[5] = (self.dirmarkpositions[5][0]-32,self.dirmarkpositions[5][1]+32)
        self.dirmarkpositions[6] = (self.dirmarkpositions[6][0]-32,self.dirmarkpositions[6][1])
        self.dirmarkpositions[7] = (self.dirmarkpositions[7][0]-32,self.dirmarkpositions[7][1]-32)

        #print self.position
        pass

    def getitem(self):
        stat = random.random()
        if stat < .25:
            self.velocity *=  1.05
        elif stat < .5:
            self.damage *= 1.15
        elif stat < .75:
            self.attackspeed *= 1.1
        else:
            self.attackrate *= .95

    def drawbottom(self, surf, camera,night):
        if self.blinking == True:
            return
        if night:
            shadowcolor = 1
        else:
            shadowcolor = 1
        surf.blit(self.scaledframes[self.frame][shadowcolor] if self.facingright else transform.flip(self.scaledframes[self.frame][shadowcolor], True, False), (self.drawrect.left+(2.0), self.drawrect.top+(2.0)))

    def drawtop(self, surf, camera,night):
        if self.blinking == True:
            return
        if night:
            spritecolor = 0
        else:
            spritecolor = 3
        surf.blit(self.scaledframes[self.frame][spritecolor] if self.facingright else transform.flip(self.scaledframes[self.frame][spritecolor], True, False), self.drawrect.topleft)
       
        surf.blit(self.directionsprites[self.frame][self.lastdirection] , self.dirmarkpositions[self.lastdirection])
